#include "Graphics.h"
#include "WZData.h"

SDL_Surface* screen;
TTF_Font *defaultFont;

////////////////////////////////////////////////////////////////
// The basic sprite class stuff
////////////////////////////////////////////////////////////////
//sprite::sprite(WZLib::WZPNGProperty* png){
//	data = png;
//	loaded = false;
//}
sprite::sprite(){
	data = 0;
	loaded = false;
	texture = 0;
	noded = false;
}
SDL_Surface* sprite::get(){
	if(data){
		if(!loaded){
			loaded = true;
			data->parse(0);
			texture = data->png();
			gltexture = OpenGL::SDL_SurfaceToTexture(this);

		}
		return texture;
	} else {
		if(!loaded){
		}
		return 0;
	}
}
GLuint sprite::glget(){
	get();
	return gltexture;
}
void sprite::operator =(WZLib::WZPNGProperty* png){
	data = png;
}
void sprite::node(void* thenode){
	if(!noded){
		noded = true;
		wz::node* tehnode = (wz::node*)thenode;
		width = (*tehnode)["width"];
		height = (*tehnode)["height"];
		originx = (*tehnode)["origin"]["x"];
		originy = (*tehnode)["origin"]["y"];
		twidth = width/pow(2,ceil(log((double)width)/log((double)2)));
		theight = height/pow(2,ceil(log((double)height)/log((double)2)));
		//cout << width << endl;
		//cout << height << endl;
		//cout << twidth << endl;
		//cout << theight << endl;
		//cout << originx << endl;
		//cout << originy << endl;
	}
}

////////////////////////////////////////////////////////////////
// Other stuff
////////////////////////////////////////////////////////////////
view_class::view_class(){
	x = 0;
	y = 0;
}
view_class view;
void init_graphics(){
	SDL_Init(SDL_INIT_EVERYTHING);
	TTF_Init();
	screen = SDL_SetVideoMode(800,600,32,SDL_DOUBLEBUF | SDL_HWSURFACE);
	SDL_WM_SetCaption("NoLifeStory",NULL); 
	SDL_ShowCursor( SDL_DISABLE );
	defaultFont = TTF_OpenFont("arial.ttf", 12);
}
void draw_sprite(sprite* spr, double x, double y, bool relative) {
	SDL_Rect offset;
	offset.x = (relative ? -view.x : 0) + x-spr->originx;
	offset.y = (relative ? -view.y : 0) + y-spr->originy;
	SDL_BlitSurface(spr->get(),NULL,screen,&offset);
	//glTranslated(-view.x,-view.y,0);
	//glTranslated(x,y,0);
	//glTranslated(-spr->originx,-spr->originy);
	//glBindTexture(GL_TEXTURE_2D,spr->get());
	//glBegin(GL_QUADS);
	//glColor4f(1,1,1,1);
	//glTexCoord2f(0,0);
	//glVertex2i(0,0);
	//glTexCoord2f(spr->twidth,0);
	//glVertex2i(spr->width,0);
	//glTexCoord2f(spr->twidth,spr->theight);
	//glVertex2i(spr->width,spr->height);
	//glTexCoord2f(0,spr->theight);
	//glVertex2i(0,spr->height);
	//glEnd();
	//glBindTexture(GL_TEXTURE_2D,0);
	//glLoadIdentity();
	//if(glGetError()!=GL_NO_ERROR){
	//	cout << "ERROR" << endl;
	//}
}

void draw_text(std::string &str, double x, double y) {
	SDL_Color clrFg = {0, 0, 255, 0};
	SDL_Surface *sText = TTF_RenderText_Solid(defaultFont, str.c_str(), clrFg);
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(sText, NULL, screen, &dest);
	SDL_FreeSurface(sText);
}